import Vue from 'vue'
import router from '@/router'
import { getuserUid } from "@/utils/auth.js";
import '@/plus/webIM/webim.config.js'
import Vuex from 'vuex'
import store from '@/store/index'
import unread from '@/utils/unread.js'
import {setRemind,getRemind,unique,setAt,getAt,fileterMsg} from '@/utils/chat.js'
import pushMsg from "@/utils/push.js";
let WebIM = require('easemob-websdk')

Vue.prototype.$webim = WebIM
const conn = new WebIM.connection({
    // isMultiLoginSessions: WebIM.config.isMultiLoginSessions,
    // https: typeof WebIM.config.https === 'boolean' ? WebIM.config.https : location.protocol === 'https:',
    // url: WebIM.config.xmppURL,
    // heartBeatWait: WebIM.config.heartBeatWait,
    // autoReconnectNumMax: WebIM.config.autoReconnectNumMax,
    // autoReconnectInterval: WebIM.config.autoReconnectInterval,
    // apiUrl: WebIM.config.apiURL,
    // isAutoLogin: true

    https: WebIM.config.https,
    url: WebIM.config.xmppURL,
    isAutoLogin: WebIM.config.isAutoLogin,
    isMultiLoginSessions: WebIM.config.isMultiLoginSessions
})
var flag = false;
const options = {
    apiUrl: WebIM.config.apiURL,
    user: null,
    pwd: null,
    appKey: WebIM.config.appkey,
    success:function (re) {
        console.log('连接成功')
        flag = true;
    },
    error:function (err) {
        console.log('连接失败')
    },
    onClosed:function(msg){
        console.log('连接关闭的回调');
    }
}
Vue.prototype.$imconn = conn
Vue.prototype.$imoption = options
Vue.prototype.$hot = false;

//封装
var msg = function(data){
    console.log(data);
    //自己发的不监听
    if(data.from != getuserUid()){
        //聊天室
        if(data.type == "chatroom"){
            store.commit("serverHot", true);
            pushMsg(data, "chatroom");
        }
        //群组
        else{
            store.commit("setHot", true);
            unread(data.to);
            //写入缓存
            let groups = getRemind()?getRemind():[];
            groups.push(data.to);
            let arr = unique(groups); //去重
            setRemind(arr); //写入缓存
            //推送
            pushMsg(data, "group");
            //判断是否有@
            if (data.ext.ifAt) {
                //如果@的是我
                let atArr = [];
                if (getAt()) {
                    atArr = getAt();
                }
                data.ext.toAt.map(o => {
                    if (o == this.userId) {
                        console.log("有人@我");
                        atArr.push(data.to);
                        let at = unique(atArr); //去重
                        setAt(at); //写入缓存
                    }
                });
            }
			//桌面图标红点
			deskHot();
        }
    }
}

//全局监听5s
var timer = setInterval(()=>{
	//监听
    if(flag){
        //消息列表、聊天室、客服不监听
        if(router.history.current.path != '/performance/msgCenter' && router.history.current.path != '/designer/msgCenter' && router.history.current.path != '/performance/chat' && router.history.current.path != '/advisory'){
            conn.listen({
                //收到文本消息
                onTextMessage:(data)=>{
                    msg(data);
                },
                //收到图片消息
                onPictureMessage:(data)=>{
                    msg(data);
                }
            })
        }
    }
	//桌面图标红点
	deskHot();
},5000)

//桌面图标红点
var deskHot = function(){
	var hot = JSON.parse(window.localStorage.getItem("unread"));
	var sum = 0;
	if(hot){
		hot.map(o=>{
			sum = sum + o.num;
		})
		plus.runtime.setBadgeNumber(sum,{
			title:"我是标题",
			content:"我是内容"
		});
	}
	else{
		plus.runtime.setBadgeNumber(0,{
			title:"我是标题",
			content:"我是内容"
		});
	}
}
